package cn.koala.key.common.jwt;

import cn.koala.key.configure.apollo.ApolloKeyManagementAdminConfig;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultCode;
import cn.seed.common.core.ResultGenerator;
import cn.seed.web.jwt.JwtTokenAuthentication;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class JwtHandlerInterceptorAdapter extends HandlerInterceptorAdapter {

	private static final Logger log = LoggerFactory.getLogger(JwtHandlerInterceptorAdapter.class);
	private static final String ENV = "dev";
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		log.info(request.getServletPath());
		if(ENV.equals(ApolloKeyManagementAdminConfig.getWorkForm())) {
			request.setAttribute("userCode", "10003");
			request.setAttribute("cxrfToken", "");
			return true;
		}else if (JwtTokenAuthentication.getAuthentication(request)) {
			return true;
		} else {
			log.warn("jwt token 验证失败，请求接口：{}，请求参数：{}", request.getRequestURI(), JSON.toJSONString(request.getParameterMap()));
			responseResult(response, ResultGenerator.genFailResult(ResultCode.UNAUTHORIZED, "jwt token 验证失败"));
			return false;
		}
	}

	private void responseResult(HttpServletResponse response, Result result) {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "application/json;charset=UTF-8");
		response.setStatus(200);
		try {
			response.getWriter().write(JSON.toJSONString(result));
		} catch (IOException ex) {
		}
	}
}
